package com.sigmatrix.h5.service.impl;

import java.util.Date;

import org.apache.commons.lang.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sigmatrix.h5.dao.DisRoleDao;
import com.sigmatrix.h5.entity.DisRole;
import com.sigmatrix.h5.service.DisRoleService;
import com.sigmatrix.h5.utils.Constants;
import com.sigmatrix.sc.jdbc.common.service.ServiceImpl;

/**
 * 
 *@ClassName: DistributionRoleServiceImpl
 *@Description: CRM角色管理
 *@author MH
 *@date 2017年11月13日 下午2:53:32
 */
@Service
public class DisRoleServiceImpl extends ServiceImpl<DisRole, Integer> implements DisRoleService {
	
	@Autowired
	private DisRoleDao distributionRoleDao;
	
	@Override
	public Integer createAdmin(Integer dealerId) {
		DisRole role = new DisRole();
		role.setCode(code());
		role.setDealerId(dealerId);
		role.setDescription("经销商反向注册自动生成");
		role.setName("超级管理员");
		role.setStatus(Constants.STATUS_ENABLE);
		role.setCreateTime(new Date());
		return insertSelective(role);
	}

	private int selectCountByCode(String code){
		DisRole role = new DisRole();
		role.setCode(code);
		return distributionRoleDao.selectCount(role);
	}
	
	/**
	 * 
	 *@Title: code
	 *@Description: 生成code
	 *@return String
	 *@author MH
	 *@date 2017年11月13日 下午3:08:06
	 *@return
	 */
	private String code(){
		String code = RandomStringUtils.random(10, "0123456789");
		while(selectCountByCode(code) > 0){
			code = RandomStringUtils.random(10, "0123456789");
		}
		return code;
	}
}
